import { defineStore } from 'pinia'
import { ref } from 'vue'
interface Product {    /*接口*/
  id: number
  name: string
  price: number
  image: string
  description: string
  category: string
  stock: number
}

export const useProductsStore = defineStore('products', () => {
  const products = ref<Product[]>([
    {
      id: 1,
      name: 'iPhone 16 Pro',
      price: 7999,
      image: '/images/products/phone-1.jpg',
      description: '全新iPhone 16 Pro，配备A17 Pro芯片，48MP主摄像头，支持USB-C接口。',
      category: '手机',
      stock: 50
    },
    {
      id: 2,
      name: 'Samsung Galaxy S24',
      price: 6999,
      image: '/images/products/phone-2.jpg',
      description: 'Samsung Galaxy S24旗舰手机，搭载最新Snapdragon处理器，超级摄影系统。',
      category: '手机',
      stock: 30
    },
    {
      id: 3,
      name: 'Xiaomi 14 Ultra',
      price: 5999,
      image: '/images/products/phone-3.jpg',
      description: '小米15 Ultra，徕卡影像系统，120W快充，旗舰性能体验。',
      category: '手机',
      stock: 25
    },
    {
      id: 4,
      name: 'OPPO Find X7 Pro',
      price: 5499,
      image: '/images/products/phone-4.jpg',
      description: 'OPPO Find X7 Pro，哈苏人像摄影系统，ColorOS 14流畅体验。',
      category: '手机',
      stock: 20
    },
    {
      id: 5,
      name: 'vivo X100 Pro',
      price: 4999,
      image: '/images/products/phone-5.jpg',
      description: 'vivo X100 Pro，蔡司光学镜头，天玑9300处理器，专业影像旗舰。',
      category: '手机',
      stock: 35
    },
    {
      id: 6,
      name: 'OnePlus 12',
      price: 4299,
      image: '/images/products/phone-6.jpg',
      description: 'OnePlus 12，骁龙8 Gen 3，100W闪充，2K 120Hz屏幕。',
      category: '手机',
      stock: 40
    }
  ])

  /*const getProductById = (id: number) => {
    return products.value.find(product => product.id === id)
  }*/
  const getProductById = (id: number) => {
  console.log('Searching for product with id:', id)
  const product = products.value.find(product => product.id === id)
  console.log('Found product:', product)
  return product
}

  const getProductsByCategory = (category: string) => {
    return products.value.filter(product => product.category === category)
  }

  return {
    products,
    getProductById,
    getProductsByCategory
  }
}) 